package com.my.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.admin.entity.Article;
import com.my.admin.service.ArticleService;
import com.my.admin.util.Rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "文章接口", tags = "文章接口")
@RestController
@RequestMapping("api")
@CrossOrigin
public class ArticleApiController {

    @Autowired
    private ArticleService service;

    @PostMapping("article/get")
    public Rest get(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, @RequestBody Article article) {
        return Rest.ok().setData("page", service.getList(new Page<>(pageNo, pageSize), article));
    }

    @GetMapping("article/search")
    public Rest get(@RequestParam String search) {
        return Rest.ok().setData("list", service.list(new QueryWrapper<Article>().like("title", search)
                .or().like("article_desc", search)
                .or().like("content", search)));
    }

    @ApiOperation(value = "根据页码获取文章列表")
    @GetMapping("article/get")
    public Rest get(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize) {
        return Rest.ok().setData("page", service.apiList(new Page<>(pageNo, pageSize)));
    }

    @ApiOperation(value = "根据Id获取文章")
    @GetMapping("article/get/{articleId}")
    public Rest get(@PathVariable Long articleId) {
        service.plusViews(articleId);
        return Rest.ok().setData("article", service.apiById(articleId));
    }

}
